package com.xhwl.centiotdata.suppliersystem.baisi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.BaiSiConstant;
import com.xhwl.centiotdata.pojo.dto.baisi.ClockRecordBaiSiDTO;
import com.xhwl.centiotdata.pojo.dto.baisi.StaffSchedulingDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 15:11 2022/1/4
 */
@Slf4j
@Service
public class ClockBaiSiHandler {

    @Autowired
    GeneralBaiSiHandler generalBaiSiHandler;

    public List<ClockRecordBaiSiDTO> pullClockRecord(PedestrianIntegrationConfigDTO configDTO, Integer offSet, Integer limit){
        LocalDateTime startDate = getStartDate(offSet);
        LocalDateTime endDate = getEndDate(startDate,limit);
        Map<String,Object> params = new HashMap<>();
        params.put("queryBeginTime",startDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        params.put("queryEndTime",endDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        List<String> list = new ArrayList<>();
        list.add("Absenteeism");
        list.add("Normal");
        list.add("Late");
        list.add("Early");
        params.put("clockStatusList",list);

        String path = configDTO.getIpAndPort() + BaiSiConstant.PULL_CLOCK_RECORD;
        JSONObject result = generalBaiSiHandler.post(path, params);
        int code = result.getInteger("code");
        if(code != 0){
            log.error("【BaiSi post】path:{};params:{};response code error!", path, JSON.toJSONString(params));
            throw new BusinessException();
        }
        return result.getJSONArray("data").toJavaList(ClockRecordBaiSiDTO.class);
    }

    /**
     * 拉取排班员工列表
     * @param configDTO
     * @return
     */
    public List<StaffSchedulingDTO> pullSchedulingStaff(PedestrianIntegrationConfigDTO configDTO, Integer offSet, Integer limit){
        LocalDateTime startDate = getStartDate(offSet);
        LocalDateTime endDate = getEndDate(startDate,limit);
        List<String> queryDateList = new ArrayList<>();
        queryDateList.add(startDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        while (endDate.isAfter(startDate)){
            startDate = startDate.plusDays(1);
            queryDateList.add(startDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        Map<String,Object> params = new HashMap<>();
        params.put("queryDateList",queryDateList);

        String path = configDTO.getIpAndPort() + BaiSiConstant.PULL_SCHEDULING_STAFF;
        JSONObject result = generalBaiSiHandler.post(path, params);
        int code = result.getInteger("code");
        if(code != 0){
            log.error("【BaiSi post】path:{};params:{};response code error!", path, JSON.toJSONString(params));
            throw new BusinessException();
        }
        return result.getJSONArray("data").toJavaList(StaffSchedulingDTO.class);
    }

    /**
     * 获取n天前的开始时间
     * @param offSet
     * @return
     */
    private LocalDateTime getStartDate(Integer offSet){
        LocalDateTime localDateTime = LocalDateTime.now();
        if(offSet != null&&offSet>0){
            localDateTime = localDateTime.minusDays(offSet);
        }
        return localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    /**
     * 获取结束时间 默认为开始时间后一天
     * @param startDate
     * @param limit
     * @return
     */
    private LocalDateTime getEndDate(LocalDateTime startDate,Integer limit){
        LocalDateTime endDate = startDate.plusDays(1);
        if(limit != null&&limit>0){
            endDate = startDate.plusDays(limit);
        }
        return endDate;
    }

}
